//
//  UserTheLabelController.m
//  CheekiOS
//
//  Created by mac on 2017/6/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserTheLabelController.h"

@interface UserTheLabelController (){
    UIScrollView *remenView;
    CGFloat btnYY;
    UIButton *but;
    
    NSMutableArray *selectArr;
    
    NSArray *labelArr;
    
    NSMutableArray *labelArrTitle;
}

@end

@implementation UserTheLabelController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavgationBarHiddenNo];
    [self selfTitleFontColor];
    
    self.title = @"个性标签选择";
    
    selectArr = [NSMutableArray array];
    labelArr = [NSMutableArray array];
    labelArrTitle = [NSMutableArray array];
    
    UIButton *commitBtn = [UIButton new];
    [commitBtn setTitle:@"确定" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    commitBtn.frame = CGRectMake(0, 0, 40, 40);
    commitBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [commitBtn addTarget:self action:@selector(clickRitghButton) forControlEvents:UIControlEventTouchUpInside];
    [commitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:commitBtn];
    
    
    [self requestDate];
}

- (void)clickRitghButton{
    if(labelArrTitle.count==0){
        [SVProgressHUD showInfoWithStatus:@"请选择相应的技能!"];
        return ;
    }
    NSMutableString *strM = [NSMutableString string];
    if(labelArrTitle.count!=0){
        for(int i = 0 ;i<labelArrTitle.count;i++){
            [strM appendString:(NSString *)labelArrTitle[i]];
            [strM appendString:@","];
        }
    }
    if(strM.length!=0){
        strM = [strM substringToIndex:strM.length-1].copy;
    }

    NSDictionary *param = @{
                            @"uid":[HQHttpManager getUserId],
                            @"skill":strM
                            };

    [HcardHttpmanager MakePersionalInfoWithParam:param success:^(id sucess) {
        
        if ([sucess[@"code"] isEqualToString:@"ok"]) {

            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSError *error) {
        
    }];
}


- (void)requestDate{

    [HcardHttpmanager getSkillsKindsSuccess:^(id sucess) {
        NSString *str = sucess;
        
        if (str.length) {

            labelArr = [str componentsSeparatedByString:@","];
        }
        
        [self setreMenUI];

    } failure:^(NSError *error) {
        
    }];
}


-(void)setreMenUI{
    
    remenView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-49)];
    remenView.backgroundColor = Colorbackground;
    remenView.userInteractionEnabled = YES;
    [self.view addSubview:remenView];
    
    
    
    //================
    
    UIImageView *shuimg= [[UIImageView alloc]initWithFrame:CGRectMake(15, 17, 3, 20)];
    shuimg.backgroundColor = [UIColor greenColor];
//    [remenView addSubview:shuimg];
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(shuimg.frame), KScreenWidth, 20)];
    lab.text = @"(最多可选4个标签)";
    lab.textColor = [UIColor blackColor];
    lab.textAlignment = NSTextAlignmentCenter;
    lab.font = [UIFont systemFontOfSize:13];
    [remenView addSubview:lab];
    

    
    float butX = 15;
    float butY = CGRectGetMaxY(shuimg.frame)+10*SCREEN_WIDTH_RATIO55;
    for(int i = 0; i < labelArr.count; i++){
        
        CGFloat frame_w = 100*SCREEN_WIDTH_RATIO55;
        
        int number = arc4random() % 2;
        
        if (number == 0) {
            
            frame_w = 85*SCREEN_WIDTH_RATIO55;
            
        }else{
            
            frame_w = 55*SCREEN_WIDTH_RATIO55;
            
        }
        
        if (butX+frame_w+20>remenView.frame.size.width*1.3-15) {
            
            butX = 30;
            
        
            
            butY = btnYY+45*SCREEN_WIDTH_RATIO55;
            
        }
        
        but = [[UIButton alloc]initWithFrame:CGRectMake(butX, butY, frame_w,frame_w)];
        [but setTitle:labelArr[i] forState:UIControlStateNormal];
        [but setTitleColor:ColorI(0xfdd815) forState:UIControlStateNormal];
        [but setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [but setBackgroundColor:[UIColor whiteColor]];
        but.titleLabel.font = [UIFont systemFontOfSize:13];
        but.layer.cornerRadius = but.frame.size.height/2;
        [but addTarget:self action:@selector(butAction:) forControlEvents:UIControlEventTouchUpInside];
        but.tag = i+100;
        
        [remenView addSubview:but];
        
        butX = CGRectGetMaxX(but.frame)+10*SCREEN_WIDTH_RATIO55;
        btnYY = CGRectGetMaxY(but.frame);
        remenView.contentSize = CGSizeMake(KScreenWidth*1.3, 0);
        
        
    }
    
}

- (void)butAction:(UIButton *)button{
    
    if (!button.selected) {
        if (selectArr.count >= 4) {
            
            [button setBackgroundColor:[UIColor whiteColor]];
            [button setTitleColor:ColorI(0xfdd815) forState:UIControlStateSelected];
            button.selected = !button.selected;
            
        }else{
            
            [button setBackgroundColor:ColorI(0xfab000)];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

            [selectArr addObject:[NSString stringWithFormat:@"%ld",button.tag-100]];
            [labelArrTitle addObject:[NSString stringWithFormat:@"%@",button.titleLabel.text]];
            
            CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
            
                animation.values = @[@1.5,@0.8,@1.0,@1.2,@1.0];
                animation.duration = 0.5;
     
            animation.calculationMode = kCAAnimationCubic;
            
            [button.layer addAnimation:animation forKey:@"transform.scale"];
            
        }
    }else{
        if (selectArr.count >= 4) {

            if ([selectArr containsObject:[NSString stringWithFormat:@"%ld",button.tag-100]]) {
                [button setBackgroundColor:[UIColor whiteColor]];
                [button setTitleColor:ColorI(0xfdd815) forState:UIControlStateSelected];
                [selectArr removeObject:[NSString stringWithFormat:@"%ld",button.tag-100]];
                [labelArrTitle removeObject:[NSString stringWithFormat:@"%@",button.titleLabel.text]];
            }
        }else{
            [button setBackgroundColor:[UIColor whiteColor]];
            if ([selectArr containsObject:[NSString stringWithFormat:@"%ld",button.tag-100]]) {
                [selectArr removeObject:[NSString stringWithFormat:@"%ld",button.tag-100]];
                [labelArrTitle removeObject:[NSString stringWithFormat:@"%@",button.titleLabel.text]];

            }
        }
    }

    button.selected = !button.selected;
    
}



@end
